package com.itheima02.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Demo3_Thread {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建Callable类的实现类对象
        Callable<Object> callable = new Ticket<>();
        //使用FutureTask包装Callable对象，该FutureTask对象封装了Callable对象call方法的返回值
        FutureTask<Object> futureTask = new FutureTask<Object>(callable);
        //创建线程对象，参数为FutureTask对象
        Thread t = new Thread(futureTask,"线程1");
        //启动线程
        t.start();
        //获取线程执行完毕后的结果
        System.out.println(futureTask.get());
    }
}

class Ticket<String> implements Callable<Object> {

    @Override
    public Object call() throws Exception {
        for (int i = 1; i <= 10; i++) {
            System.out.println(i);
        }
        return Thread.currentThread().getName()+"线程执行完毕"; //返回线程执行结果
    }
}
